package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.HopInventoryView;
import Models.MaltInventoryView;
import Models.RawGoodsInvReport;
import Models.WarehouseInventory;

public class RawGoodsInvReportPresenter extends AbstractPresenter<RawGoodsInvReport> {
	final String SINGLEITEM = "rawGoodsInventoryReport";
	final String MULTIITEM = "rawGoodsInventoryReports";
	
	public ArrayList<RawGoodsInvReport> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public RawGoodsInvReport tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public RawGoodsInvReport createItemFromJSONObject(JSONObject jsonObject){
		RawGoodsInvReport item = new RawGoodsInvReport();
		try{
			//Set Malts Collection
			ArrayList<MaltInventoryView> malts = new ArrayList<MaltInventoryView>();
			if(!jsonObject.isNull("maltCollection")){
				Object itm = jsonObject.get("maltCollection");
				MaltInventoryViewPresenter presenter = new MaltInventoryViewPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						malts.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					malts.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setMaltCollection(malts);
			}
			//Set Hops Collection
			ArrayList<HopInventoryView> hops = new ArrayList<HopInventoryView>();
			if(!jsonObject.isNull("hopColletion")){
				Object itm = jsonObject.get("hopColletion");
				HopInventoryViewPresenter presenter = new HopInventoryViewPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						hops.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					hops.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setHopColletion(hops);
			}
			//Set Warehouse Collection
			ArrayList<WarehouseInventory> whs = new ArrayList<WarehouseInventory>();
			if(!jsonObject.isNull("whCollection")){
				Object itm = jsonObject.get("whCollection");
				WarehouseInventoryPresenter presenter = new WarehouseInventoryPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						whs.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					whs.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setWhCollection(whs);
			}
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(RawGoodsInvReport item){
		try{
			JSONObject json = new JSONObject();
			//Create Doc Collection
			if(item.getMaltCollection()!=null){
				MaltInventoryViewPresenter presenter = new MaltInventoryViewPresenter();
				if(item.getMaltCollection().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getMaltCollection().size(); i++){
						jsonArray.put(presenter.createJSON(item.getMaltCollection().get(i)));
					}
					json.put("maltCollection", jsonArray);
				}
			}
			//Create Doc Collection
			if(item.getHopColletion()!=null){
				HopInventoryViewPresenter presenter = new HopInventoryViewPresenter();
				if(item.getHopColletion().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getHopColletion().size(); i++){
						jsonArray.put(presenter.createJSON(item.getHopColletion().get(i)));
					}
					json.put("hopColletion", jsonArray);
				}
			}
			//Create Doc Collection
			if(item.getWhCollection()!=null){
				WarehouseInventoryPresenter presenter = new WarehouseInventoryPresenter();
				if(item.getWhCollection().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getWhCollection().size(); i++){
						jsonArray.put(presenter.createJSON(item.getWhCollection().get(i)));
					}
					json.put("whCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public RawGoodsInvReport getEmptyEntity(){
		RawGoodsInvReport entity = new RawGoodsInvReport();
		return entity;
	}
}
